<?php

$config = include "common.php";

$image_name = $argv[1] ?? '';
if (empty($image_name)) {
    exit('您还没选择下载哪一个相册呢！');
}
$image_info = json_decode(file_get_contents('db.txt'), true);
if (empty($image_info)) {
    exit('您还未获取您的相册！');
}
$image_detail = $image_info[$image_name] ?? [];
if (empty($image_detail)) {
    exit('输入的相册有误！');
}

$image_id = $image_detail['id'];
$total = $image_detail['total'];
$pageNum = 30;
$counts = ceil($total / $pageNum);

for ($i = 0; $i < $counts; $i++) {
    $pageStart = $i * $pageNum;
    $img_list = "https://h5.qzone.qq.com/proxy/domain/photo.qzone.qq.com/fcgi-bin/cgi_list_photo?g_tk={$config['g_tk']}&hostUin={$config['uin']}&topicId={$image_id}&uin={$config['uin']}&pageStart={$pageStart}&pageNum={$pageNum}&inCharset=utf-8&outCharset=utf-8&format=json";
    $httpHeaders = array(
        "cookie:" . $config['cookie']
    );

    $httpClient = new HttpClient($img_list, 'GET');
    $httpClient->setRequestHeader($httpHeaders);
    $httpClient->exec();
    $responseBody = json_decode($httpClient->getResponseBody(), true);

    if ($responseBody['code'] == 0) {
        $image_list = $responseBody['data']['photoList'];
        if (empty($image_list)) {
            exit('您相册是空的哦！');
        }
        $image_name = [];
        foreach ($image_list as $v) {
            $content = curl_file_get_contents($v['url']);
            file_put_contents('my_photo/' . $v['name'] . '.jpg', $content);
        }
        $num = count($image_list);
        echo "已下载完成{$num}\n";
    } else {
        exit('下载异常已终止');
    }
    sleep(2);
}
exit('下载结束');


